<template>
  <div class="orderListContainer">
    <div class='navbar' v-if='show'><mp-navbar :tabs="tabs" :activeIndex='activeIndex' @tabClick="tabClick"></mp-navbar></div>
    <div class='info-card-wrap'>
        <scroll-view  style="height:100%" :scroll-y="true" @scrolltolower="scrolltolower">
            <DeviceCard v-for='(item,index) in dataList' :key='index' :item='item' :hide='index==0' cardType='0' @selectDevice='toDetail'/>
        </scroll-view>
    </div>
  </div>
</template>

<script type="text/javascript">
import mpNavbar from 'mpvue-weui/src/navbar';
import DeviceCard from '@/components/device_card/device_card.vue';
import { mapState,mapActions,mapGetters  } from 'vuex';
export default {
  data() {
    return {
      tabs:['待派工单','待接工单','已接工单','待审工单'],
      activeIndex:0,
      dataList:[],
      unsentOrders: [],
      unacceptedOrders:[],
      acceptedOrders:[],
      finishedOrders:[], 
      minId:0,
      sendTime:0,
      orderId:null,
      url:'/order/getUnsentOrders',
      urls:['/order/getFaultOrder','/order/getInspectOrder','/order/getMaintenanceOrder','/order/getAffairOrder'],
      userId:null,//不为空为下属管理的工单
      projectId:null,//不为空为工单统计的工单
      orderType:0,
      startTime:'',
      endTime:'',
      show:true,//tabbar是否显示
    }
  },
  onShow() {
      var query = this.$root.$mp.query;
      this.userId = null;
      if(query.userId) {
        this.userId = query.userId;
        this.tabs = ['已接工单','完成工单'];
        this.show = true;
        this.activeIndex = 0;
        wx.setNavigationBarTitle({
            title: '下属' 
        })
      }else if(query.projectId){
        this.tabs = [];
        this.show = false;
        this.projectId = query.projectId;
        this.orderType = query.orderType;
        this.startTime = query.startTime;
        this.endTime = query.endTime;
        wx.setNavigationBarTitle({
            title: query.title 
        })
      }else{
        this.show = true;
        this.tabs =['待派工单','待接工单','已接工单','待审工单'];
      }
      var pages = getCurrentPages();
      var currPage = pages[pages.length - 1]; //当前页面
      var params = currPage.data;
      if(params.orderId) {
        this.orderId = params.orderId;
        var index = -1;
        this.dataList.map((order,i)=>{
            if(order.orderId == this.orderId){
                index = i;
            }
        })
        console.log('index',index)
        if(index>-1) this.dataList.splice(index,1)
      }
  },
  mounted(){
      this.getNetwork()
      this.tabClick(this.activeIndex)
  },
  methods:{
      ...mapActions([
          'setOrderList',
          'getOrderList',
          'setOrderDetail',
          'getNetwork'
      ]),
      tabClick(index){
        this.dataList = [];
        this.minId = 0;
        this.activeIndex = index;
        if(this.userId){
            if(index==0){
                this.url = '/order/getAcceptedOrdersByUser';
            }else if(index==1){
                this.url = '/order/getFinishedOrdersByUser'
            }
        }else if(this.projectId){
            this.url = '/statistics/getOrderList';
        }else{
            if(index==0){
                this.url = '/order/getUnsentOrders';
            }else if(index==1){
                this.sendTime = 0;
                this.url = '/order/getUnacceptedOrders2';
            }else if(index==2){
                this.url = '/order/getAcceptedOrders2';
            }else if(index==3){
                this.sendTime = 0;
                this.url = '/order/getFinishedOrders2';
            }
        }
        if(this.isConnected){
            this.queryOrder()
        }else{
            if(!this.userId){
                if(this.activeIndex==0){
                    this.dataList = this.unsentOrders
                }else if(this.activeIndex==1){
                    this.dataList = this.unacceptedOrders;
                }else if(this.activeIndex==2){
                    this.dataList = this.acceptedOrders;
                }else if(this.activeIndex==3){
                    this.dataList = this.finishedOrders;
                }
            }
        }
      },
      queryOrder(){
           var data={
                ukey:this.userInfo.ukey,
                startId:this.minId
          }
          if(this.userId){
               data.userId = this.userId;
          }else if(this.projectId){
               data.projectId = this.projectId;
               data.startTime = this.startTime;
               data.endTime = this.endTime;
               data.orderId = this.minId;
               data.orderType = this.orderType;
          }else{
             if(this.activeIndex==1){
                  data.sendTime = this.sendTime;
                  data.groupId = this.userInfo.groupId;
              }
              if(this.activeIndex==3){
                  data.finishTime = this.sendTime;
                  data.groupId = this.userInfo.groupId;
              }
              if(this.activeIndex==2){
                  data.acceptTime = this.sendTime;
              }
          }
          this.$http.post({
            url:this.url,
            data
           }).then((res)=>{
                if(res.s==0){
                    this.dataList = this.dataList.concat(res.i.Data);
                    if(!this.userId){
                        if(this.activeIndex==0){
                            if(this.minId==0) this.unsentOrders = [];
                            this.unsentOrders = this.unsentOrders.concat(res.i.Data);
                        }else if(this.activeIndex==1){
                            if(this.minId==0) this.unacceptedOrders = [];
                            this.unacceptedOrders = this.unacceptedOrders.concat(res.i.Data);
                        }else if(this.activeIndex==2){
                            if(this.minId==0) this.acceptedOrders = [];
                            this.acceptedOrders = this.acceptedOrders.concat(res.i.Data);
                            this.dataList.map(order=>{
                                if(!this.orderDetails[order.orderId]){
                                    this.queryOrderInfo(order.orderType,order.orderId)
                                }
                            })
                        }else if(this.activeIndex==3){
                            if(this.minId==0) this.finishedOrders = [];
                            this.finishedOrders = this.finishedOrders.concat(res.i.Data);
                        }
                    }
                    //this.setOrderList({type:this.activeIndex,list:this.dataList})
                    
                }
           }).catch((e)=>{
                console.log('请求失败',e)
           })
      },
      queryOrderInfo(type,orderId){
        this.$http.post({
            url:this.urls[type],
            data:{
                ukey:this.userInfo.ukey,
                orderId:orderId,
            }
        }).then((res)=>{
            if(res.s==0){
                this.setOrderDetail(res.i.Data);
            }
        }).catch((e)=>{
            console.log('请求失败',e)
        })
      },
      scrolltolower(){
          if(this.isConnected){
            var minIdObj = this.dataList.reduce((prev,cur)=>{
                return prev.orderId<cur.orderId ? prev : cur;
            })
            if(this.activeIndex!=0) this.sendTime = this.dataList[this.dataList.length-1].orderTime;
            this.minId = minIdObj.orderId;
            this.queryOrder() 
          }
      },
      toDetail(orderId,orderType){
          var url = `/pages/order_detail/main?orderId=${orderId}&orderType=${orderType}&activeIndex=${this.activeIndex}`;
          if(this.userId) url+=`&showBtn=0`;
          wx.navigateTo({url}) 
      },
  },    
  components: {
    mpNavbar,
    DeviceCard
  },
  computed:{
    ...mapState(['userInfo']),
    ...mapGetters([
        'isConnected',
        'orderDetails'
    ])

  }
}
</script>

<style lang='less'>
page{
    background:#eef7fd;
    height:100%;
}
::-webkit-scrollbar{
    width: 0;
    height: 0;
    color: transparent;
}
.weui-navbar{
    z-index:1;
}
.weui-navbar-slider-on{
    width: 100%!important;
    background-color: #1d7ebf!important;
}
.weui-navbar__item{
    padding: 16rpx 0!important;
    color:#7b7b7b;
    &.weui-bar__item_on{
    color: #1d7ebf!important;
    }
}
.weui-tab{
    border-bottom:none!important;
    background: #fff!important;
}
.orderListContainer{
    height: 100%;
    display: flex;
    flex-direction: column;
    .info-card-wrap{
        height: 50rpx;
        flex:1;
    }
}
</style>
